#include "stdafx.h"
#include "UGlobalGLFunctions.h"
#include "UDockable.h"

PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL;					// VBO Name Generation Procedure
PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL;					// VBO Bind Procedure
PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL;					// VBO Data Loading Procedure
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;

void gluGetRenderParams(gluRenderParams& params) {
	glGetDoublev(GL_MODELVIEW_MATRIX, params.modelViewMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, params.projectionMatrix);
	glGetIntegerv(GL_VIEWPORT, params.viewport);
}

TVector projectPoint(const TVector& v, gluRenderParams &params, TDockable * window) { // Return Global Coords
	GLdouble returnValue[3];
	::gluProject(v.x, v.y, v.z, \
	            params.modelViewMatrix, params.projectionMatrix, params.viewport,
	            &returnValue[0], &returnValue[1], &returnValue[2]);
	if(window)
		return TVector(returnValue[0] - window->Left, (ScreenHeight - returnValue[1]) - window->Top, returnValue[2]);
	else
		return TVector(returnValue[0], (ScreenHeight - returnValue[1]), returnValue[2]);
}

TVector unProjectPoint(const TVector& v, gluRenderParams &params, TDockable * window = NULL) {
	GLdouble modelViewMatrix[16];
	GLdouble projectionMatrix[16];
	GLint    viewport[4];
	GLdouble returnValue[3];

	glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
	glGetIntegerv(GL_VIEWPORT, viewport);

	::gluProject(v.x, v.y, v.z, \
	            modelViewMatrix, projectionMatrix, viewport,
	            &returnValue[0], &returnValue[1], &returnValue[2]);

// 	return TVector(returnValue[0] - Left , (ScreenHeight - returnValue[1]) - Top, returnValue[2]); //FIXME left, top?
	return TVector(returnValue[0], (ScreenHeight - returnValue[1]), returnValue[2]); //FIXME?
}
